﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;
using System.Collections;


namespace Quanly_dethi.Class
{
    class csC_MonThi
    {
        private ketnoi kn;

        internal ketnoi Kn
        {
            get { return kn; }
            set { kn = value; }
        }

        private string databasename;

        public string Databasename
        {
            get { return databasename; }
            set { databasename = value; }
        }
        public csC_MonThi()
        {
            Kn = new ketnoi();
            databasename = "";
        }
        public csC_MonThi(string servername, string user, string pass, string dbname)
        {
            kn = new ketnoi(servername, user, pass);
            databasename = dbname;
        }
        public void Insert(csE_MonThics x)
        {
            Database db = Kn._Server.Databases[databasename];
            string s="insert into MonThi(TenMon) values(N'" + x.TenMon.ToString() + "')";
            db.ExecuteWithResults(s);
            //Database db = Kn._Server.Databases[databasename];
            //string s = "insert into Lop(Tenlop) values (N'" + x.Tenlop.ToString() + "')";
            //db.ExecuteWithResults(s);
        }
        public DataTable GetMonThi(csE_MonThics u)
        {
            Database db = Kn._Server.Databases[databasename];
            string s = "select top 1 MaMon, TenMon, Malop from MonThi order by MaTS DESC";
            DataTable k = db.ExecuteWithResults(s).Tables[0];
            return k;
        }
        public Boolean update(string duongdan)
        {
            Database db = Kn._Server.Databases[databasename];
            string s = "RESTORE DATABASE [TTN] FROM  DISK =" + "'" + duongdan + "'" + " WITH  FILE = 1,  NOUNLOAD,  STATS = 10";
            DataTable k = db.ExecuteWithResults(s).Tables[0];
            if (k.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
       
        public DataTable selectalll()
        {
            Database db = Kn._Server.Databases[databasename];
            return db.ExecuteWithResults("select MaMon,TenMon,Tenlop from MonThi mt,Lop lp where mt.Malop=lp.Malop").Tables[0];
            
        }
        public void ThemMon(int MaMon, string TenMon, int Malop)
        {
            try
            {
                Database db = Kn._Server.Databases[databasename];
                string sql = "insert Table_KetQua values('" + MaMon + "','" + TenMon + "','" + Malop + "')";
                // string s = "insert into Nguoidung(TenND, MaQuyen,Matkhau,Xoa) values('" + x.Tennguoidung.ToString() + "','" + int.Parse(x.Maquyen.ToString()) + "','" + x.Matkhau.ToString() + "','" + int.Parse(x.Xoa.ToString()) + "')";
            }
            catch (Exception)
            {
                throw new Exception("Không thể thêm");
            }
        }
        public void Update(csE_MonThics x)
        {
            string t = "update MonThi set TenMon='" + x.TenMon + "', Malop='" + x.Malop + "' where MaMon='" + x.MaMon + "'";
            Database db = Kn._Server.Databases[databasename];
            db.ExecuteWithResults(t);
        }
    }
}

    